<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { sendResetPasswordEmail, forgotPassword } from '../../api/auth'
import type { ResetEmailRequest } from '../../types/auth'
import { Message, Back, Check } from '@element-plus/icons-vue'

const router = useRouter()

// 表单数据
const emailForm = reactive<ResetEmailRequest>({
  email: ''
})

// 表单验证规则
const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email' as const, message: '请输入正确的邮箱格式', trigger: 'blur' }
  ]
}

// 表单引用
const formRef = ref()

// 提交状态
const isSubmitting = ref(false)
const isSubmitted = ref(false)

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    isSubmitting.value = true
    
    try {
      // 使用新的API函数
      const response = await forgotPassword(emailForm.email)
      
      if (response.success) {
        isSubmitted.value = true
        ElMessage.success('重置密码邮件已发送，请检查您的邮箱')
      } else {
        ElMessage.error(response.message || '发送失败，请稍后再试')
      }
    } catch (error: any) {
      console.error('发送邮件失败:', error)
      
      if (error.response && error.response.data) {
        ElMessage.error(error.response.data.message || '发送失败，请稍后再试')
      } else {
        ElMessage.error('发送失败，请稍后再试')
      }
    }
  } catch (error: any) {
    console.error('表单验证失败:', error)
  } finally {
    isSubmitting.value = false
  }
}

// 返回登录页面
const goToLogin = () => {
  router.push({ name: 'Login' })
}
</script>

<template>
  <div class="forgot-page">
    <div class="forgot-container">
      <div class="forgot-form-wrapper">
        <div class="forgot-header">
          <h1 class="forgot-title">忘记密码</h1>
          <p class="forgot-subtitle" v-if="!isSubmitted">请输入您的电子邮箱，我们将发送重置密码的链接</p>
        </div>
        
        <div v-if="!isSubmitted" class="forgot-form-container">
          <el-form
            ref="formRef"
            :model="emailForm"
            :rules="rules"
            label-position="top"
            class="forgot-form"
            @keyup.enter="handleSubmit"
          >
            <el-form-item prop="email">
              <el-input
                v-model="emailForm.email"
                placeholder="请输入您的邮箱"
                size="large"
                :prefix-icon="Message"
                class="custom-input"
                clearable
              />
            </el-form-item>
            
            <el-button
              type="primary"
              class="submit-btn"
              size="large"
              :loading="isSubmitting"
              @click="handleSubmit"
            >
              发送重置邮件
            </el-button>
          </el-form>
          
          <div class="form-footer">
            <div class="divider">
              <span>或者</span>
            </div>
            
            <el-button
              type="default"
              class="back-btn"
              size="large"
              @click="goToLogin"
            >
              <el-icon class="mr-2"><Back /></el-icon>
              返回登录
            </el-button>
          </div>
        </div>
        
        <div v-else class="success-message">
          <div class="success-icon-wrapper">
            <el-icon class="success-icon"><Check /></el-icon>
          </div>
          <h2 class="success-title">邮件已发送</h2>
          <p class="success-text">
            重置密码邮件已发送到您的邮箱。请在10分钟内完成密码重置操作。
          </p>
          <p class="success-help">
            如果没有收到邮件，请检查垃圾邮件文件夹，或在几分钟后重试。
          </p>
          
          <el-button
            type="primary"
            class="back-login-btn"
            size="large"
            @click="goToLogin"
          >
            返回登录
          </el-button>
        </div>
      </div>
      
      <div class="forgot-banner">
        <div class="banner-content">
          <h2 class="banner-title">账户安全</h2>
          <p class="banner-text">我们采用了多种安全措施保护您的账户安全，强密码是抵御黑客攻击的第一道防线。</p>
          <div class="banner-features">
            <div class="feature-item">
              <div class="feature-icon"><i class="el-icon-lock"></i></div>
              <div class="feature-text">邮箱验证机制</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon"><i class="el-icon-message"></i></div>
              <div class="feature-text">快速重置密码</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon"><i class="el-icon-refresh"></i></div>
              <div class="feature-text">随时更新账户信息</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.forgot-page {
  min-height: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f8fafc;
  padding: 1rem;
  box-sizing: border-box;
}

.forgot-container {
  display: flex;
  width: 100%;
  max-width: 1000px;
  min-height: min(600px, 90vh);
  height: auto;
  background-color: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
}

.forgot-form-wrapper {
  flex: 1;
  padding: 2.5rem;
  display: flex;
  flex-direction: column;
}

.forgot-header {
  margin-bottom: 2rem;
  text-align: center;
}

.forgot-title {
  font-size: 2rem;
  font-weight: 600;
  color: #1a202c;
  margin-bottom: 0.5rem;
}

.forgot-subtitle {
  color: #718096;
  font-size: 0.95rem;
}

.forgot-form-container {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.forgot-form {
  margin-bottom: 2rem;
}

.custom-input :deep(.el-input__wrapper) {
  padding: 0 15px;
  height: 48px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  border: 1px solid #e2e8f0;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #13c2c2;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px rgba(19, 194, 194, 0.2);
  border-color: #13c2c2;
}

.submit-btn {
  width: 100%;
  height: 48px;
  font-weight: 600;
  border-radius: 6px;
  background-color: #13c2c2;
  border-color: #13c2c2;
  transition: all 0.2s;
}

.submit-btn:hover {
  background-color: #36cfc9;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(19, 194, 194, 0.2);
}

.form-footer {
  margin-top: auto;
}

.divider {
  display: flex;
  align-items: center;
  color: #a0aec0;
  margin: 1.5rem 0;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  border-bottom: 1px solid #e2e8f0;
}

.divider::before {
  margin-right: 1rem;
}

.divider::after {
  margin-left: 1rem;
}

.back-btn {
  width: 100%;
  height: 48px;
  font-weight: 600;
  border-radius: 6px;
}

.mr-2 {
  margin-right: 0.5rem;
}

.success-message {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 2rem 1rem;
  flex: 1;
}

.success-icon-wrapper {
  width: 80px;
  height: 80px;
  background-color: rgba(19, 194, 194, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 1.5rem;
}

.success-icon {
  font-size: 40px;
  color: #13c2c2;
}

.success-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: #1a202c;
  margin-bottom: 1rem;
}

.success-text {
  color: #4a5568;
  margin-bottom: 1rem;
  max-width: 400px;
}

.success-help {
  color: #718096;
  font-size: 0.9rem;
  max-width: 400px;
  margin-bottom: 2rem;
}

.back-login-btn {
  width: 200px;
  height: 48px;
  font-weight: 600;
  border-radius: 6px;
  background-color: #13c2c2;
  border-color: #13c2c2;
}

.forgot-banner {
  flex: 1;
  background: linear-gradient(135deg, #13c2c2 0%, #006064 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.banner-content {
  position: relative;
  z-index: 2;
  padding: 2.5rem;
  text-align: center;
}

.banner-title {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 1rem;
}

.banner-text {
  font-size: 1.1rem;
  opacity: 0.9;
  margin-bottom: 2.5rem;
  line-height: 1.6;
}

.banner-features {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.feature-item {
  display: flex;
  align-items: center;
  text-align: left;
}

.feature-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 1rem;
  font-size: 1.2rem;
}

.feature-text {
  font-weight: 500;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .forgot-container {
    flex-direction: column-reverse;
    max-width: 500px;
    height: auto;
    min-height: 0;
  }
  
  .forgot-banner {
    display: none;
  }
  
  .forgot-form-wrapper {
    padding: 1.5rem;
  }
}

@media (max-width: 576px) {
  .forgot-form-wrapper {
    padding: 1.5rem;
  }
  
  .forgot-title {
    font-size: 1.75rem;
  }
}

@media (max-height: 700px) {
  .forgot-container {
    min-height: 0;
    margin: 1rem 0;
  }
  
  .forgot-form-wrapper {
    padding: 1.5rem;
  }
}
</style>

<style>
:root {
  --primary-color: #13c2c2;
  --primary-light: #36cfc9;
  --primary-dark: #008f8f;
  --bg-color: #f0f2f5;
  --sidebar-width: 220px;
  /* 其他变量... */
}
</style>

<style>
.app-wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  width: 100%;
  flex: 1;
  overflow: auto; /* 内容溢出时可滚动 */
}

.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  min-height: 0; /* 重要：允许flex子项收缩 */
}

.main-content {
  flex: 1;
  overflow: auto;
  padding: 24px;
}
</style> 